Docker compose
What is docker compose
Syntax
services:
web:
build: .
ports:
- "8000:5000"
- "8888:8888"
redis:
image: "redis:alpine"
Note. This cannot be executed directly.
Manage multiple containers
https://docs.docker.com/compose/gettingstarted/
Quick start
docker-compose.yaml
services:
flask_web_server:
build:
context: .
dockerfile: flask.Dockerfile
container_name: flask_web_server
restart: always
ports:
- "80:5000"
volumes:
- .:/app
demo_ngrok:
image: ngrok/ngrok
container_name: demo_ngrok
depends_on:
- flask_web_server
restart: always
environment:
- NGROK_AUTHTOKEN=<your_ngrok_authtoken>
ports:
- "54088:4040"
links:
- flask_web_server:http
command: http flask_web_server:5000
flask.Dockerfile
FROM --platform=linux/amd64 python:3.11-slim-bullseye
WORKDIR /workspace
COPY . /workspace
ENV TZ=Asia/Taipei
ENV FLASK_APP=app.py
ENV FLASK_RUN_HOST=0.0.0.0
EXPOSE 5000
RUN apt-get update -y
RUN apt-get install curl vim wget procps git -y
RUN apt-get install -y zsh \
&& echo "Y" | sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
RUN pip install --upgrade pip
RUN pip install flask
CMD ["flask", "run"]
app.py
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "<h1>Hello!</h1>"
if __name__ == "__main__":
app.run()